﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AnalisisDeRiesgos.Dal;

namespace AnalisisDeRiesgos.Models.User
{
    public class DetailsUserModel
    {
        public int CI { get; set; } // The UserId
        public String FirstName { set; get; } // The First Name of the user      
        public String LastName { set; get; } // The Last name of the user
        public String SecondLastName { set; get; } // The second last name of the user

        private riskanalysisEntities entities = new riskanalysisEntities(); // A reference to Entities

        public void FillUser(int id)
        {
            var userList = from u in entities.users
                           where u.CI == id
                           select u;

            if (userList.Count() <= 0)
                throw new DetailsUserException("El usuario especificado no fue encontrado");

            user theUser = userList.First();

            CI = theUser.CI;
            FirstName = theUser.firstName;
            LastName = theUser.lastName;
            SecondLastName = theUser.secondLastName;
        }

        public void DeleteUser(int actualCI)
        {
            var userList = from u in entities.users
                           where u.CI == CI && u.enabled
                           select u;

            if (userList.Count() <= 0)
                throw new DetailsUserException("El usuario especificado no fue encontrado");

            if (CI == actualCI)
                throw new DetailsUserException("No puede eliminarse a si mismo");

            userList.First().enabled = false;
            entities.SaveChanges();
        }
    }
}